package com.white.note.adapter;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.white.note.R;
import com.white.note.entity.NoteInfo;

import java.util.ArrayList;
import java.util.List;

public class NoteListAdapter extends RecyclerView.Adapter<NoteListAdapter.MyHolder> {
    List<NoteInfo> mNoteInfoList = new ArrayList<>();

    public void setNoteInfoList(List<NoteInfo> mNoteInfoList) {
        this.mNoteInfoList = mNoteInfoList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_list_item, parent, false);
        return new MyHolder(view);
    }

    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        // 绑定数据
        NoteInfo noteInfo = mNoteInfoList.get(position);
        // 设置数据
        holder.tv_note_title.setText(noteInfo.getNote_title());
        holder.tv_note_content.setText(noteInfo.getNote_content());
        holder.tv_note_create_time.setText(noteInfo.getNote_create_time());
        String[] split = noteInfo.getNote_create_time().split("-");
        holder.tv_month.setText(split[1] + "月");
        holder.tv_date.setText(split[2]);

        Typeface typeface;
        if (noteInfo.getNote_is_top() == 1) {
            holder.iv_note_top.setVisibility(View.VISIBLE);
            typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD);
        } else {
            holder.iv_note_top.setVisibility(View.GONE);
            typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL);
        }
        holder.tv_note_title.setTypeface(typeface);
        holder.tv_note_content.setTypeface(typeface);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnItemClickListener) {
                    mOnItemClickListener.onItemClick(position, noteInfo);
                }
            }
        });

        holder.iv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnItemClickListener) {
                    mOnItemClickListener.onMoreClick(position, noteInfo);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mNoteInfoList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {
        TextView tv_note_title;
        TextView tv_note_content;
        TextView tv_note_create_time;
        TextView tv_month;
        TextView tv_date;
        ImageView iv_more;
        ImageView iv_note_top;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tv_note_title = itemView.findViewById(R.id.tv_note_title);
            tv_note_content = itemView.findViewById(R.id.tv_note_content);
            tv_note_create_time = itemView.findViewById(R.id.tv_note_create_time);
            tv_month = itemView.findViewById(R.id.tv_month);
            tv_date = itemView.findViewById(R.id.tv_date);
            iv_more = itemView.findViewById(R.id.iv_more);
            iv_note_top = itemView.findViewById(R.id.iv_note_top);

        }
    }

    private onItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(onItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface onItemClickListener {

        void onItemClick(int position, NoteInfo noteInfo);

        void onMoreClick(int position, NoteInfo noteInfo);
    }
}
